package org.example.utils;

import org.apache.commons.lang.StringUtils;
import org.example.model.Device;
import org.openqa.selenium.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DeviceUtils {

    /**
     * 打开网页后调用
     * 此处只需要初始化一次
     * @param webDriver
     */
    public static Device initDeviceInfo(WebDriver webDriver) {
        Device device = new Device();
        final WebElement deviceNameEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/div/div[1]/h1"));
        if (deviceNameEle != null) {device.setName(deviceNameEle.getText());}

        final WebElement ipEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div[1]/div[2]/div[2]"));
        if (ipEle != null) {device.setIpAddr(ipEle.getText());}

        final WebElement androidVersionEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div[2]/div[1]/div[2]"));
        if (androidVersionEle != null) {device.setAndroidVersion(androidVersionEle.getText());}

        final WebElement batteryEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div[2]/div[2]/div[2]"));
        if (batteryEle != null) {device.setBattery(batteryEle.getText());}

        final WebElement storageEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div[3]/div[1]/div[2]"));
        if (storageEle != null) {device.setStorageInfo(storageEle.getText());}

        final WebElement memoryEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div[3]/div[2]/div[2]"));
        if (memoryEle != null) {device.setMemoryInfo(memoryEle.getText());}

        // 去屏幕镜像获取设备宽高
        final WebElement ImageEle = webDriver.findElement(By.xpath("/html/body/div/div/div/nav/div/div/a[4]"));
        if (ImageEle != null) {ImageEle.click();}
        GlobalUtils.sleep(3000);
        // 开始投屏
        final WebElement startEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/div/div[2]/button[2]"));
        if (startEle != null) {startEle.click();}
        GlobalUtils.sleep(3000);

        final WebElement imageEle = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/div/div/div[2]/div[1]"));
        Point imageEleLocation = imageEle.getLocation();
        device.setOffsetX(imageEleLocation.getX());
        device.setOffsetY(imageEleLocation.getY());
        if (imageEle != null) {
            String regex = "(\\d+)(\\.\\d+)?px";
            Pattern pattern = Pattern.compile(regex);
            String width = imageEle.getCssValue("width");
            String height = imageEle.getCssValue("height");
            if (StringUtils.isNotEmpty(width)) {
                Matcher matcher = pattern.matcher(width);
                if (matcher.find()) {
                    String extractedNumber = matcher.group(1);
                    device.setWidth(Integer.parseInt(extractedNumber));
                }
            }
            if (StringUtils.isNotEmpty(height)) {
                Matcher matcher = pattern.matcher(height);
                if (matcher.find()) {
                    String extractedNumber = matcher.group(1);
                    device.setHeight(Integer.parseInt(extractedNumber));
                }
            }
        }

        return device;
    }
}
